package demo.practice.leetcode_eveyday;

import java.util.*;

public class P703 {


}

class KthLargest {

    public static void main(String[] args) {
        KthLargest kthLargest = new KthLargest(3, new int[]{1,1});
        System.out.println(kthLargest.add(1));
        System.out.println(kthLargest.add(1));
        System.out.println(kthLargest.add(3));
        System.out.println(kthLargest.add(3));
        System.out.println(kthLargest.add(3));
        System.out.println(kthLargest.add(4));
        System.out.println(kthLargest.add(4));
        System.out.println(kthLargest.add(4));
    }

    private int k;
    private List<Integer> list = new ArrayList<>();

    public KthLargest(int k, int[] nums) {
        this.k = k;
        Arrays.stream(nums).forEach(i -> list.add(i));
        list.sort((i, j) -> j - i);
    }

    public int add(int val) {

        if (list.size()==0||val <= list.get(list.size() - 1))
            list.add(val);
        else
            for (int i = 0; i < list.size(); i++) {
                if (val > list.get(i)) {
                    list.add(i, val);
                    break;
                }
            }

        if (k > list.size())
            return -1;

        return list.get(k-1);
    }
}

